//
//  ECMineCourseFinishVC.m
//  Edu_iOS2018
//
//  Created by apple on 2018/1/8.
//  Copyright © 2018年 yanyan. All rights reserved.
//

#import "ECMineCourseFinishVC.h"
#import "ECMineCourseTableViewCell.h"
#import "ECMineCourseLearnModel.h"

#import "CourseModel.h"
#import "ECUIVC_CourseDetail.h"

@interface ECMineCourseFinishVC ()<UITableViewDataSource,UITableViewDelegate,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>

@property (nonatomic, strong) UITableView *finishTBview;
@property (nonatomic, strong) NSMutableArray *finishTBArray;
///总个数
@property (nonatomic, assign) int allPageNum;
@end

@implementation ECMineCourseFinishVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.finishTBview];
    [self getDataWithURL];
    
}

/*  加载数据  */
-(void)getDataWithURL{
    WeakSelf(weakSelf);
    [HWHttpTool requestWithMethod:@"GET"
                              url:URL_GET_Me_Courses
                           params:@{@"relation":@"learned",
                                    @"start":@(self.finishTBArray.count)
                                    }
                          headers:@{@"X-AUTH-TOKEN":[[UserInfoManager shareUser] token]}
                          success:^(id json) {
                              [weakSelf handleViewWithData:json];
                          }
                          failure:^(NSError *error) {
                              [self.finishTBview.mj_footer endRefreshing];
                              NSString *str = [ECData_Json requestErrorTipWith:error];
                              if (!([str isEqualToString:@"101"] || [str isEqualToString:@"401"])) {
                                  [MBProgressHUD showTipMessageInWindow:kAlertTipOfInternetFailure];
                              }
                          }];
}
- (void)handleViewWithData:(id)json
{
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        self.allPageNum = [json[@"data"][@"total"] intValue];//总个数
        NSArray *array = json[@"data"][@"resources"];
        [self.finishTBArray addObjectsFromArray: [ECMineCourseLearnModel mj_objectArrayWithKeyValuesArray:array]];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.finishTBview.mj_footer endRefreshing];//结束刷新
            
            if (self.finishTBArray.count >= self.allPageNum) {
                [self.finishTBview.mj_footer endRefreshingWithNoMoreData];//隐藏控件
                self.finishTBview.mj_footer.hidden = YES;
                
            } else {
                [self.finishTBview.mj_footer resetNoMoreData];
                self.finishTBview.mj_footer.hidden = NO;
            }
            [self.finishTBview reloadData];
        });
    });
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.finishTBArray.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *indetify = @"ECMineCourseTableViewCell";
    ECMineCourseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indetify];
    if (!cell) {
        cell = [[ECMineCourseTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indetify];
    }
    [cell hiddenProgressViewWithTag:tableView.tag];
    [cell setContentMode:self.finishTBArray[indexPath.row]];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CourseModel *courseModel = [[CourseModel alloc]init];
    ECMineCourseLearnModel *model = self.finishTBArray[indexPath.row];
    courseModel.modelId = model.ID;
    ECUIVC_CourseDetail *vc = [[ECUIVC_CourseDetail alloc]init];
    vc.object = courseModel;
    [self.navigationController pushViewController:vc animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kProportion_Width(162+48*2+24);
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
    return [UIImage imageNamed:@"Mine_emptyImage_MineFinish"];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{
    NSString *text = @"暂无已学完的课程";
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:16.0f],
                                 NSForegroundColorAttributeName: [UIColor colorWithHexString:@"b3b3b3"]};
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

-(UITableView *)finishTBview{
    if (!_finishTBview) {
        _finishTBview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-kProportion_Height(138 + 12) - kNavHeight-kSafeAreaBottomHeight) style:UITableViewStylePlain];
        _finishTBview.backgroundColor = [UIColor whiteColor];
        _finishTBview.delegate = self;
        _finishTBview.dataSource = self;
        _finishTBview.emptyDataSetSource = self;
        _finishTBview.emptyDataSetDelegate = self;
        _finishTBview.separatorStyle = 0;
        _finishTBview.tag = 11;
        _finishTBview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            
                [self getDataWithURL];
        }];
    }
    return _finishTBview;
}

-(NSMutableArray *)finishTBArray{
    if (!_finishTBArray) {
        _finishTBArray = [[NSMutableArray alloc]init];
    }
//    //判断数组个数,隐藏footer
//    if (_finishTBArray.count == 0) {
//        self.finishTBview.mj_footer.hidden = YES;
//    }else{
//        self.finishTBview.mj_footer.hidden = NO;
//    }
    return _finishTBArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
